package demo.day09;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/1-08-01-16:31
 * @Description：demo.day09
 */
class Add implements Computer{

    @Override
    public int computer(int n, int m) {
        return n + m;
    }
}
class Multiply implements Computer{

    @Override
    public int computer(int n, int m) {
        return n * m;
    }
}
class Subtract implements Computer{

    @Override
    public int computer(int n, int m) {
        return n - m;
    }
}
class Except implements Computer{

    @Override
    public int computer(int n, int m) {
        if( m == 0){
            throw new RuntimeException("除数不能为0");
        }
        return n / m;
    }
}
class UseCompute{
    public static void useCom(Computer com, int one,int two){
        int result = com.computer(one,two);
        if(com instanceof Add){
            System.out.printf("%d + %d  = %d\n",one,two,result);
        }else if(com instanceof Multiply){
            System.out.printf("%d * %d = %d\n",one,two,result);
        }else if(com instanceof Subtract){
            System.out.printf("%d -%d = %d\n",one,two,result);
        }else if(com instanceof Except){
            System.out.printf("%d / %d = %d\n",one,two,result);
        }else{
            System.out.println("输出的com类型有误，无法计算");
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Computer com1 = new Add();
        UseCompute.useCom(com1,1,2);
        Computer com2 = new Multiply();
        UseCompute.useCom(com2,2,3);
        Computer com3 = new Subtract();
        UseCompute.useCom(com3,4,1);
        Computer com4 = new Except();
        UseCompute.useCom(com4,3,0);
    }
}
